<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Generator" content="Kate, the KDE Advanced Text Editor" />
<title>ind.c</title>
</head>
<body>
<pre style='color:#1f1c1b;background-color:#ffffff;'>
<i><span style='color:#898887;'>//== SAGITTARIUS ============================================================================</span></i>

<i><span style='color:#898887;'>//== BEGINNING OF CODE ===============================================================</span></i>

<span style='color:#006e28;'>#include </span><span style='color:#006e28;'>&lt;stdio.h&gt;</span>
<span style='color:#006e28;'>#include </span><span style='color:#006e28;'>&lt;string.h&gt;</span>
<span style='color:#006e28;'>#include </span><span style='color:#006e28;'>&lt;malloc.h&gt;</span>

<b>typedef</b> <b>struct</b> {
    <span style='color:#0057ae;'>char</span>* str;
    size_t l;
} lenstr_t;

<i><span style='color:#898887;'>//int is used for type of &quot;chr&quot; because getc returns int</span></i>
lenstr_t* append(lenstr_t* a, <span style='color:#0057ae;'>int</span> chr)
    <b>if</b>(chr == EOF) {
        <b>if</b>(a) {
            free(a-&gt;str);
            free(a);
        }
        <b>return</b> NULL;
    } <b>else</b> {
        size_t l = a ? a-&gt;l : <span style='color:#b08000;'>0</span>; <i><span style='color:#898887;'>//base length</span></i>
        lenstr_t* r=malloc(<b>sizeof</b>(lenstr_t));
        r-&gt;str = malloc(l+<span style='color:#b08000;'>1</span>);
        <b>if</b>(a) {
            memcpy(r-&gt;str, a-&gt;str, a-&gt;l);
            free(a-&gt;str);
            free(a); <i><span style='color:#898887;'>//a is no longer needed</span></i>
        }
        r-&gt;str[l] = chr;
        r-&gt;l = l+<span style='color:#b08000;'>1</span>;
        <b>return</b> r;
    }
}

<span style='color:#0057ae;'>int</span> main(<span style='color:#0057ae;'>int</span> argc, <span style='color:#0057ae;'>char</span>* argv[]) {
    lenstr_t* a=NULL;
    <b>while</b>((a = append(a, getc(stdin))))
        printf(<span style='color:#bf0303;'>&quot;%.*s</span><span style='color:#ff80e0;'>\n</span><span style='color:#bf0303;'>&quot;</span>, a-&gt;l, a-&gt;str);
    
    <b>return</b> <span style='color:#b08000;'>0</span>;
}
</pre>
</body>
</html>
